package sis.studentinfotest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-26 09:35
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.studentinfo.Course;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseTest extends TestCase {
    public void testCreate() {
        Course course = new Course("CMSC", "120");
        assertEquals("CMSC", course.getDepartment());
        assertEquals("120", course.getNumber());
    }

    public void testEquality() {
        Course courseA = new Course("NURS", "201");
        Course courseAprime = new Course("NURS", "201");
        assertEquals(courseA, courseAprime);

        Course courseB = new Course("NURS", "201");
        assertEquals(courseA, courseB);

        assertEquals(courseA, courseA);

        Course courseAprime2 = new Course("NURS", "201");
        assertEquals(courseAprime, courseAprime2);
        assertEquals(courseA, courseAprime2);

        assertEquals(courseAprime, courseA);

        assertEquals(courseA, courseAprime);

        assertFalse(courseA.equals(null));

        assertTrue(courseA == courseA);

        assertSame(courseA, courseA);

        assertTrue(courseA.equals(courseA));

        List<Course> list = new ArrayList<>();
        list.add(courseA);
        assertTrue(list.contains(courseAprime));

        Map<Course, String> map = new HashMap<>();
        map.put(courseA, "");
        assertTrue(map.containsKey(courseAprime));
    }

    public void testHahsAndEq() {
        Course courseA = new Course("NURS", "201");
        Course courseB = new Course("NURS", "201");
        Map<Course, String> map = new HashMap<>();
        map.put(courseA, "");
        map.put(courseB, "");
        assertEquals(1, map.size());
    }

    public void testHashCode() {
        Integer i = 2;
        Integer x = 2;
        assertTrue(i.hashCode() == x.hashCode());
        assertTrue(i == x);
        assertEquals(2, i.hashCode());
        String s1 = "你好";
        String s2 = "你好";
        String s3 = new String("你好");
        String s4 = new String("你好");
        assertTrue(s1.hashCode() == s2.hashCode());
        assertTrue(s1.hashCode() == s3.hashCode());
        assertTrue(s4.hashCode() == s3.hashCode());
        assertFalse(s4 == s3);

        Course courseA = new Course("NURS", "201");
        Course courseAprime = new Course("NURS", "201");

        Map<Course, String> map = new HashMap<>();
        map.put(courseA, "");
        assertTrue(map.containsKey(courseAprime));

        assertEquals(courseA.hashCode(), courseAprime.hashCode());

        assertEquals(courseA.hashCode(), courseA.hashCode());

        assertFalse(courseA.equals("CMSC-120"));
    }

    public void testHahsCodePerformance() {
        final int count = 10000;
        long start = System.currentTimeMillis();
        Map<Course, String> map = new HashMap<>();
        for (int i = 0; i < count; i++) {
            Course course = new Course("A" + i, "" + i);
            map.put(course, "");
        }
        long stop = System.currentTimeMillis();
        long elapsed = stop - start;
        final long arbitraryThreshold = 200;
        assertTrue(elapsed < arbitraryThreshold);
    }

    public void testeToString(){
        Course course = new Course("ENGL","301");
        assertEquals("ENGL 301",course.toString());
        assertEquals("Course: ENGL 301","Course: "+course);
    }
}
